<?php
/**
 * TaskConsumer
 * 异步任务消费者
 */

namespace Selibra\Task;

use Selibra\Event\EventTrigger;
use Selibra\Task\Protocol\TaskEvent;
use Selibra\Tools\Console;

class Consumer
{

    /**
     * @param $server
     * @param $data
     * @return mixed
     */
    public function exec($server, \Swoole\Server\Task $data)
    {
        $message = $data->data;
        if ($message instanceof TaskEvent) {
            EventTrigger::listener($message);
        } elseif ($message instanceof TaskMessage) {
            // 执行钩子
            return $message->getTaskConsumer()->exec($message->getData());
        }
    }


    /**
     * 回收内存
     */
    public function __destruct()
    {
        gc_collect_cycles();
    }

}